//
//  LZFWordPostController.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/7/8.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFWordPostController.h"
#import "LZFPlaceholderTextView.h"
#import "LZFTagToolView.h"

@interface LZFWordPostController ()<UITextViewDelegate>
@property(nonatomic,weak)LZFPlaceholderTextView *placeHolderView;
@property(nonatomic,weak)LZFTagToolView *tagToolView;
@end

@implementation LZFWordPostController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航栏属性
    [self setUpNav];
    
    //设置uitextview
    [self setUpTextView]; 
    
     //设置通知 监听textview的改变
     //方案二:可以用textview的代理方法 - (void)textViewDidChange:(UITextView *)textView; 监听到
      //textview的文字的改变
    [self setUpNote];
    
    //谁知tagTool工具条
    [self setUpTagTool];
    
}

//设置tagTool工具条
-(void)setUpTagTool
{
    LZFTagToolView *tagToolView=(LZFTagToolView *)[LZFTagToolView loadXibView];
    self.tagToolView=tagToolView;
    tagToolView.xmg_width=screenW;
    tagToolView.xmg_y=screenH-tagToolView.xmg_height;
    [self.view addSubview:tagToolView];
    
    
}

//监听通知
-(void)setUpNote
{  //监听uitextview发出的通知,修改发表按钮的状态
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:nil];
    
    
    //监听键盘frame的改变
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

-(void)keyBoardFrameChange:(NSNotification *)notice
{
    //LZFLog(@"%@",notice.userInfo);
    //键盘弹出的动画时间
CGFloat duration  = [(notice.userInfo[UIKeyboardAnimationDurationUserInfoKey]) doubleValue];
    
    //键盘弹出来后的frame -- Y
CGFloat keyboardEndY=[(notice.userInfo[UIKeyboardFrameEndUserInfoKey]) CGRectValue].origin.y;
    
    
    //对tagToolView进行动画
 
    [UIView animateWithDuration:duration animations:^{
          CGFloat y =screenH -keyboardEndY;
        self.tagToolView.transform=CGAffineTransformMakeTranslation(0, -y);
        
        
    }];
    
}



//通知监听响应
-(void)textDidChange:(NSNotification *)notice
{
    //修改导航栏发表按钮的状态
    self.navigationItem.rightBarButtonItem.enabled=((UITextView *)notice.object).hasText;
}

//移除通知
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

//设置导航栏属性
-(void)setUpNav
{
    self.view.backgroundColor=[UIColor whiteColor];
    
    self.title=@"发表文字";
    
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(clickCancle)];
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"发表" style:UIBarButtonItemStylePlain target:self action:@selector(clickPost)];
    
    self.navigationItem.rightBarButtonItem.enabled=NO;
    
    //强制刷新navigationBar自己和子控件的所有内容(尺寸 状态)
    [self.navigationController.navigationBar layoutIfNeeded];
    

}

//view 出现前让uitextview交出键盘
-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
     [self.placeHolderView becomeFirstResponder];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //只要一滚动textview,就退出键盘
    [self.placeHolderView resignFirstResponder];
}


 //设置uitextview
-(void)setUpTextView
{
    LZFPlaceholderTextView *placeHolderTextView=[[LZFPlaceholderTextView alloc]init];
    self.placeHolderView=placeHolderTextView;
    placeHolderTextView.delegate=self;
    placeHolderTextView.frame=self.view.bounds;
    placeHolderTextView.alwaysBounceVertical=YES; //即使scrollview的content范围比frame小,也总是有垂直弹簧效果
    [self.view addSubview:placeHolderTextView];
}

//点击取消按钮
-(void)clickCancle
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//点击发布按钮
-(void)clickPost
{

    LZFLog(@"post");
}

#pragma mark 点击 tagToolView的+按钮


@end
